/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.github.ngocnh210.TrueName.Events;

import com.github.ngocnh210.TrueName.TrueName;
import com.github.ngocnh210.TrueName.Util.Config;
import com.github.ngocnh210.TrueName.Util.Util;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerLoginEvent;

/**
 *
 * @author NgocNH
 */

public class TruenamePlayerListener extends PlayerListener {
    public TrueName plugin;

    public TruenamePlayerListener(TrueName instance) {
        plugin = instance;
    }

    public void onPlayerLogin(PlayerLoginEvent event) {
        Player player = event.getPlayer();
        if (Config.filter_action.equalsIgnoreCase("kick") || Config.filter_action.equalsIgnoreCase("rename")) {
            String name = player.getName();
            if (Util.checkFilter("username", name) == false) {
                if (Config.filter_action.equalsIgnoreCase("kick")) {
                    Config.has_badcharacters = true;
                    event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Util.replaceStrings(Config.filter_username, player, null));
                    Config.has_badcharacters = false;
                    return;
                }
            }
            if (player.getName().length() < Integer.parseInt(Config.username_minimum_length)) {
                event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Util.replaceStrings(Config.message_username_minimum, player, null));
            } else if (player.getName().length() > Integer.parseInt(Config.username_maximum_length)) {
                event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Util.replaceStrings(Config.message_username_maximum, player, null));
            }
        }
    }
}
